package com.example.demo.service;

import com.example.demo.entity.JsonResult;
import com.example.demo.entity.WorkTable;
import com.example.demo.mapper.CustomerMapper;
import com.example.demo.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService{

    @Autowired
    MemberMapper memberMapper;
    @Autowired
    CustomerMapper customerMapper;

    @Override
    public JsonResult startWorkMember(WorkTable workTable) {
        JsonResult json = new JsonResult();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmSSS");
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        workTable.setStartTime(sd.format(date));
        workTable.setUpdateTime(sd.format(date));
        //同时在两个平行表中生成开始数据（表1记录总的数据，表2记录临时数据）
        //todo 首先确认表一中是否拥有未完成的订单
        String tableCode = customerMapper.getTableCode(workTable.getPhone());
        if(null==tableCode){
            workTable.setTableCode("WORK"+sdf.format(date));
            //todo 记录表1插入数据
            //开始时间
            memberMapper.startWorkMemberMain(workTable);
            //todo 临时表表2插入数据
            memberMapper.startWorkMemberVice(workTable);
        }else {
            //todo 临时表表2插入数据
            workTable.setTableCode(tableCode);
            memberMapper.startWorkMemberVice(workTable);
        }
        json.setSuccess(true);
        json.setMessage("开始工作");
        return json;
    }

    @Override
    public JsonResult endWorkMember(WorkTable workTable) {
        JsonResult json = new JsonResult();
        WorkTable workTable1 = customerMapper.getWorkTable(workTable.getPhone());
        Date date = new Date();
        Date now = new Date(System.currentTimeMillis());
        Date start = customerMapper.getUpdateTime(workTable.getPhone());
        long startTime = start.getTime();
        long nowTime = now.getTime();
        long allTime = (nowTime-startTime)/1000;
        workTable.setAllTime(Integer.valueOf(String.valueOf(allTime)));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        workTable1.setEndTime(sdf.format(date));
        workTable1.setAllTime((int) allTime+workTable1.getAllTime());
        memberMapper.endWorkMemberVice(workTable1);// 更新副表
        // 获取副表的时间和  填入主表
        Integer sumTime = customerMapper.getSumTime(workTable1.getTableCode());
        workTable1.setAllTime(sumTime);
        memberMapper.endWorkMemberMain(workTable1);// 只更新
        json.setSuccess(true);
        json.setMessage("结束工作");
        return json;
    }

    @Override
    public JsonResult updateWorkMember(WorkTable workTable) {
        JsonResult json = new JsonResult();
        Date start = customerMapper.getUpdateTime(workTable.getPhone());
        Date now = new Date(System.currentTimeMillis());
        long startTime = start.getTime();
        long nowTime = now.getTime();
        long allTime = (nowTime-startTime)/1000;
        workTable.setAllTime(Integer.valueOf(String.valueOf(allTime)));
        Date date =  new Date();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        workTable.setUpdateTime(sd.format(date));
        //更新副表
        memberMapper.updateMemberWorkVice(workTable);
        // 获取副表的时间和  填入主表
        Integer sumTime = customerMapper.getSumTimeUp(workTable.getPhone());
        workTable.setAllTime(sumTime);
        //更新主表
        memberMapper.updateMemberWorkMain(workTable);

        //更新钱包
        customerMapper.updateAWallet(workTable.getPhone(),workTable.getEarnMoney());
        json.setSuccess(true);
        json.setMessage("更新成功");
        return json;
    }

    @Override
    public JsonResult getWorkMember(WorkTable workTable) {
        return null;
    }

    @Override
    public JsonResult judgeWorkMember(String phone) {
        JsonResult json = new JsonResult();
        Map<String,Object> map = new HashMap<>();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //todo 查询是否存在有为完成12小时工作时间的订单
        WorkTable workTable = customerMapper.getWorkTable1(phone);
        if (workTable==null){
            //todo 查询工作的结束时间，如果不超过10小时，不能进行
            long nowTime = date.getTime();
            Date endTime = customerMapper.getEndTime(phone);
            if (endTime != null&&(nowTime-endTime.getTime())/1000<36000) {
                map.put("is_have",1);
                map.put("allowTime",(36000-(nowTime-endTime.getTime())/1000));
                map.put("msg","结束没有超过10小时，请10小时后再工作，还剩"+(36000-(nowTime-endTime.getTime())/1000));
            } else {
                map.put("is_have",2);
                map.put("allowTime", 12*3600);
                map.put("msg","还没有开始工作！！");
            }
        }else {
            //查询是否有正在运行的订单
            WorkTable workTable1 = customerMapper.getWorkTableIng(phone);
            //查询已用的工作时间（或剩余的工作时间）
            Integer userTime = customerMapper.getUserTime(workTable1.getTableCode());
            if(workTable1==null){
                map.put("is_have",3);

                map.put("allowTime", 12*3600-userTime);
                map.put("workedMoney",workTable.getEarnMoney());
                map.put("msg","已开始开始工作！！还剩余"+( 60-workTable.getAllTime())+"秒试运行时间");
            }else {
                map.put("is_have",4);
                map.put("allowTime", 12*3600-userTime);
                map.put("WorkTable",workTable1);
                map.put("workedMoney",workTable1.getEarnMoney());
                map.put("msg","订单正在进行");
            }
        }
        json.setData(map);
        json.setSuccess(true);
        return json;
    }
    @Override
    public void save(String phone,Integer amount){
        memberMapper.save(phone, amount);
    }
}
